# from exts import db
# from common import constant
# from app.order.model_mixin import OrderModelMixin
# from common.base_model import BasicModelMixin, CompanyMixin, CreateMixin, UpdateMixin, DeleteMixin
# from common import errors
# from app.storage.storage_record.model import StorageRecord
# from app.storage.storage_flow.model import StorageFlow
# from common.constant import OrderStatus, OrderType
# from app.finance.account_flow.model import AccountFlow
# import datetime
# from app.file.model import FileRelation
#
#
# class PurchaseOrderModelMixin:
#
#     # def save_draft(self, order_skus_json, image_ids, **kwargs):
#     #     if self.status != OrderStatus.DRAFT:
#     #         raise errors.BadRequestError('无法保存草稿，这不是一个草稿单！')
#     #     # 更新order
#     #     self.update(**kwargs)
#     #     # 更新 order_sku
#     #     for order_sku in self.order_skus:
#     #         order_sku.hard_delete()  # 先删
#     #     for order_sku_json in order_skus_json:
#     #         PurchaseOrderSku.create(order_id=self.id, **order_sku_json)  # 后增
#     #
#     #     # 更新images
#     #     self.update_images(image_ids)
#     #     db.session.commit()
#
#     def update_images(self, image_ids):
#         if image_ids or len(image_ids) == 0:
#             # 删除旧的
#             old_relations = FileRelation.query.filter_by(
#                 belong_id=self.id,
#                 belong_type=constant.FileBelongType.PURCHASE_ORDER).all()
#             for old_relation in old_relations:
#                 old_relation.hard_delete()
#             # 创建新的
#             for image_id in image_ids:
#                 FileRelation.create(
#                     file_id=image_id, belong_id=self.id,
#                     belong_type=constant.FileBelongType.PURCHASE_ORDER)
